04. DELETE a Todo item - Exercise

DELETE a todo item Heading

DELETE a todo item: The "D" in CRUD

We're almost done with CRUD!

Learning how to handle deletes in our application implements the last operation of the 4 operations in CRUD.

ND004 C01 L07 04 DELETE A Todo Item

Takeaways

Deletes deal with removing existing objects in our database

In SQL:

DELETE FROM table_name
WHERE condition;

In SQLAlchemy ORM:

todo = Todo.query.get(todo_id) 
db.session.delete(todo) # or...
Todo.query.filter_by(id=todo_id).delete()
db.session.commit()

Steps we'll implement:

  • Loop through every To-Do item and show a delete button
  • Pressing the delete button sends a request that includes which to-do item to delete
  • The controller takes the user input, and notifies the models to delete the To-Do object by ID
  • On successful deletion by the models, the controller should notify the view to refresh the page and redirect to our homepage, showing a fresh fetch of all To-Do items to now exclude the removed item.

Using the DELETE method

Requests that delete objects should use the method DELETE , as opposed to POST , GET , etc. when sending requests to the server.

ND004 C01 L07 05 DELETE Exercise Walthrough

You can use the interactive workspace below to complete your To-Do app.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter-lab
  • Opened files (when workspace is loaded): n/a

Task Description:

Implement the following to successfully delete To-Do items in your app

Task List:

Task Feedback:

Awesome job! Compare your implementation to the proposed solution located in the next section.

Try to implement deletion on your own before looking at the solution

When you are finished, or if you get stuck, check out the solution located in the next section.